<?php
defined('BASEPATH') or exit('No direct script access allowed');
require APPPATH.'libraries/ApiController.php';
class roles extends ApiController
{
	public function __construct()
	{
	    parent::__construct();
	}
	function index(){
		 
	}

	public function list_by_page(){
		$token=123;//$this->verifyToken();
		if($token){
			$list=$this->db->select("roleid,rolename,desc,enable")->get("auth_roles")->result_array();
			$this->WRITE("code",1);
			$this->WRITE("list",$list);
			$this->WRITE("msg","success");
			$this->RESPONSE();
		}
	}
	
	public function detail(){
 			$token=$this->verifyToken();
			if($token){
				$data=$this->getKeys([
				  ['roleid',True,"角色id"]],'post');
				$role=$this->db->where("roleid",$data["roleid"])->select("roleid,rolename,desc,enable,permission")->get("auth_roles")->row_array();
				
				//选中项处理
				$rights=$role["permission"]==""? []:explode(",",$role["permission"]);
				$role["permission"]=$rights;
				
				$this->WRITE("code",1);
				$this->WRITE("detail",$role);
				$this->WRITE("msg","success");
				$this->RESPONSE();
			}
	}
	
	public function update(){
		$token=$this->verifyToken();
		if($token){
			$data=$this->getKeys([
			  ['roleid',True,"角色id"],
			  ['rolename',True,"角色名称"],
			  ['enable',True,"启用禁用状态"],
			  ['permission',False,"权限列表",""]
			  ],'post');
			  
			$res=$this->db->where("roleid",$data["roleid"])->update("auth_roles",$data);
			if($res){
				$this->WRITE("code",1);
				$this->WRITE("msg","编辑角色成功!");
				$this->RESPONSE();
			}else{
				$this->WRITE("code",0);
				$this->WRITE("msg","编辑角色失败!");
				$this->RESPONSE();
			} 
		}
	}
}